package utils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class FindBook {
    static Connection con = null;
    static PreparedStatement preSql;
    static ResultSet rs;

    FindBook() {
    }

    /**
     * 显示所有图书
     *
     * @param model
     */
    public static void allbook(DefaultTableModel model) {
        try {
            String sqlStr = "select * from books_table";
            con = JDBCUtil.getConnect();
            preSql = con.prepareStatement(sqlStr);
            rs = preSql.executeQuery();
            while (rs.next()) {
                String name = rs.getString("name");
                String author = rs.getString("author");
                String type = rs.getString("type");
                String num = rs.getString("num");
                model.addRow(new Vector<>(Arrays.asList(name, author, type, num)));
            }
        } catch (Exception e) {
            System.out.println("NULL");
        } finally {
            JDBCUtil.close(preSql, rs, con);
            System.out.println("搜索所有图书结束");
        }
    }

    /**
     * 根据类型搜索
     *
     * @param model
     * @param BookType
     */
    public static void findbookbytype(DefaultTableModel model, String BookType) {
        System.out.println("con 开始");
        try {
            con = JDBCUtil.getConnect();
        } catch (Exception e) {
        }
        BookType = "%" + BookType + "%";
        String sqlStr = "select * from books_table where type like ?";
        try {
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, BookType);
            rs = preSql.executeQuery();
            boolean flag = false;
            int count = 0;
            while (rs.next()) {
                flag = true;
                String name = rs.getString("name");
                String author = rs.getString("author");
                String type = rs.getString("type");
                String num = rs.getString("num");
                model.addRow(new Vector<>(Arrays.asList(name, author, type, num)));
                //计算行数
                count++;
            }
            if (!flag) {
                JOptionPane.showMessageDialog(null, "图书不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
        } catch (SQLException e) {
        } finally {
            JDBCUtil.close(preSql, rs, con);
            System.out.println("类型搜索结束");
        }
    }

    /**
     * 按关键字查找图书
     *
     * @param model
     * @param key
     */
    public static void searchbykey(DefaultTableModel model, String key) {
        key = "%" + key + "%";
        String sqlStr = "select * from books_table where author like ? or name like ? or type like ?";
        try {
            con = JDBCUtil.getConnect();
            System.out.println("key 搜搜con");
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, key);
            preSql.setString(2, key);
            preSql.setString(3, key);
            rs = preSql.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                String name = rs.getString("name");
                String author = rs.getString("author");
                String type = rs.getString("type");
                String num = rs.getString("num");
                model.addRow(new Vector<>(Arrays.asList(name, author, type, num)));
            }
            if (!flag) {
                JOptionPane.showMessageDialog(null, "图书不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
            con.close();
        } catch (SQLException e) {
        } finally {
            JDBCUtil.close(preSql, rs, con);
            System.out.println("关键字搜索结束");
        }
    }

    public static String searchbyid(String id) {
        String name = "";
        String author = "";
        String num = "";
        String type = "";
        String sqlStr = "select * from books_table where id=?";
        try {
            con = JDBCUtil.getConnect();
            System.out.println("id检索con");
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1, id);
            rs = preSql.executeQuery();
            boolean flag = false;
            while (rs.next()) {
                flag = true;
                name = rs.getString("name");
                author = rs.getString("author");
                type = rs.getString("type");
                num = rs.getString("num");
                System.out.println("检索开始");
            }
            if (!flag) {

                JOptionPane.showMessageDialog(null, "图书不存在", "警告", JOptionPane.WARNING_MESSAGE);
            }
            con.close();
        } catch (SQLException e) {
            System.out.println("NULL");
        } finally {
            JDBCUtil.close(preSql, rs, con);
            System.out.println("关键字搜索结束");
        }
        return id + "-" + name + "-" + author + "-" + type + "-" + num;
    }

    public static String returnbook(String id) {
        String num = "";
        try {
            con = JDBCUtil.getConnect();
            String sql = "update books_table set num=num+1 where id=?";
            preSql = con.prepareStatement(sql);
            preSql.setString(1, id);
            preSql.executeUpdate();
            String sql2 = "select * from books_table where id=?";
            preSql = con.prepareStatement(sql2);
            preSql.setString(1, id);
            rs = preSql.executeQuery();
            while (rs.next()) {
                num = rs.getString("num");
                System.out.println("num+" + num);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return num;

    }

    public static void main(String[] args) {
        System.out.println("1");
    }
}